﻿namespace UnitFundProfitability
{
	using System;
	using System.Collections.Generic;
	using System.Windows;
	using System.Windows.Controls;
	using UnitFundProfitability.BusinessLogic;

	/// <summary>
	/// Interaction logic for UnitFunds.xaml.
	/// </summary>
	public partial class UnitFunds : Window
	{
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="UnitFunds"/> class.
		/// </summary>
		public UnitFunds()
		{
			InitializeComponent();
			ExistingCompanies.DataContext = Manager.GetManagementCompanies();
			ExistingCompanies.SelectionChanged += new SelectionChangedEventHandler(ExistingCompanies_SelectionChanged);
			ExistingUnitFunds.SelectionChanged += new SelectionChangedEventHandler(ExistingUnitFunds_SelectionChanged);
			RefreshExistingUnitFunds();
			AddUnitFund.Click += new RoutedEventHandler(AddUnitFund_Click);
			UpdateUnitFund.Click += new RoutedEventHandler(UpdateUnitFund_Click);
		}
		#endregion

		#region Methods
		/// <summary>
		/// Refreshes the existing unit funds.
		/// </summary>
		private void RefreshExistingUnitFunds()
		{
			ManagementCompany selectedManagemetnCompany = ExistingCompanies.SelectedValue as ManagementCompany;
			if (selectedManagemetnCompany != null)
			{
				IList<UnitFund> dataContext = Manager.GetUnitFunds(selectedManagemetnCompany);
				ExistingUnitFunds.DataContext = dataContext;
			}
		}

		/// <summary>
		/// Handles the SelectionChanged event of the ExistingCompanies control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
		private void ExistingCompanies_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshExistingUnitFunds();
		}

		/// <summary>
		/// Handles the SelectionChanged event of the ExistingUnitFunds control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
		private void ExistingUnitFunds_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			UnitFund unitFund = ExistingUnitFunds.SelectedItem as UnitFund;
			if (unitFund != null)
			{
				UnitFundName.Text = unitFund.FundName;
				MarkdownPeriod.Text = unitFund.MarkdownPeriod.ToString();
			}
		}

		/// <summary>
		/// Handles the Click event of the AddUnitFund control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void AddUnitFund_Click(object sender, RoutedEventArgs e)
		{
			ManagementCompany selectedManagementCompany = ExistingCompanies.SelectedValue as ManagementCompany;
			if (selectedManagementCompany == null)
			{
				MessageBox.Show("Please, select management company.", "Select company");
			}
			else
			{
				int markdownPeriod;
				if (!int.TryParse(MarkdownPeriod.Text, out markdownPeriod))
				{
					MessageBox.Show("Please, enter a valid number.", "Invalid number");
				}
				else
				{
					UnitFund unitFund = new UnitFund();
					unitFund.CompanyId = selectedManagementCompany.CompanyId;
					unitFund.FundId = Guid.NewGuid();
					unitFund.FundName = UnitFundName.Text;
					unitFund.MarkdownPeriod = markdownPeriod;
					Manager.AddUnitFund(unitFund);
					UnitFundName.Text = string.Empty;
					MarkdownPeriod.Text = string.Empty;
					RefreshExistingUnitFunds();
				}
			}
		}

		/// <summary>
		/// Handles the Click event of the UpdateUnitFund control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void UpdateUnitFund_Click(object sender, RoutedEventArgs e)
		{
			ManagementCompany selectedManagementCompany = ExistingCompanies.SelectedValue as ManagementCompany;
			if (selectedManagementCompany == null)
			{
				MessageBox.Show("Please, select management company.", "Select company");
			}
			else
			{
				int markdownPeriod;
				if (!int.TryParse(MarkdownPeriod.Text, out markdownPeriod))
				{
					MessageBox.Show("Please, enter a valid number.", "Invalid number");
				}
				else
				{
					UnitFund unitFund = ExistingUnitFunds.SelectedItem as UnitFund;
					unitFund.FundName = UnitFundName.Text;
					unitFund.MarkdownPeriod = markdownPeriod;
					Manager.UpdateUnitFund(unitFund);
					UnitFundName.Text = string.Empty;
					MarkdownPeriod.Text = string.Empty;
					RefreshExistingUnitFunds();
				}
			}
		}
		#endregion
	}
}